package com.traffic.collect.common;

import com.alibaba.fastjson.JSON;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;
import java.util.List;

/**
 * @author xm
 */
@Getter
@Setter
public class Response<T> implements Serializable {
    private static final long serialVersionUID = 6686419048438176898L;

    /**
     * 状态码
     */
    private Integer error;
    private String message;

    private List<String> messageList;

    private Long id;
    /**
     * 业务数据
     */
    private T body;

    public Response<T> error(int error) {
        this.error = error;
        return this;
    }

    public Response<T> id(Long id) {
        this.id = id;
        return this;
    }

    public Response<T> body(T body) {
        this.body = body;
        return this;
    }


    private Response() {

    }

    private Response(int status, String message, T body) {
        this.error = status;
        this.message = message;
        this.body = body;
    }
    private Response(int status, List<String> messageList, T body, String message) {
        this.error = status;
        this.messageList = messageList;
        this.body = body;
        this.message = message;
    }

    public static <T> Response<T> getInstance(T body) {
        return new Response<>(0, null, body);
    }

    public static <T> Response<T> getInstanceList(List<String> msglist,T body) {
        return new Response<>(0, msglist, body,null);
    }

    public static <T> Response<T> ok() {
        return new Response<>(0, null, null);

    }

    public static <T> Response<T> nonExpect(int error) {
        return new Response<>(error, null, null);
    }

    public static <T> Response<T> nonExpect(int error, String message) {
        return new Response<>(error, message, null);
    }

    @Override
    public String toString() {
        if (null == this.body) {
            this.body((T) new Object());
        }
        return JSON.toJSONString(this);
    }

    public Response<T> message(String message) {
        this.message = message;
        return this;
    }

    public boolean fail() {
        return this.error == null || this.error != 0;
    }

}
